package com.webside.series.controller;

 
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import org.springframework.web.bind.annotation.ResponseBody;
 
 

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
 
import com.webside.base.basecontroller.BaseController;
import com.webside.dtgrid.model.Pager;
import com.webside.dtgrid.util.ExportUtils;
import com.webside.exception.AjaxException;
import com.webside.exception.ServiceException;
import com.webside.exception.SystemException;
import com.webside.listener.BaseData;
import com.webside.listener.vo.Channel;

import com.webside.series.model.Bdictionary;
import com.webside.series.model.BvideoEntity;
import com.webside.series.model.SeriesEntity;
import com.webside.series.service.BdictionaryService;
import com.webside.series.service.BvideoService;
import com.webside.series.service.SeriesService;
 
import com.webside.util.Common;
import com.webside.util.PageUtil;


@Controller
@Scope("prototype")
 
@RequestMapping("/series/")
public class SeriesController extends BaseController {
 
	@Autowired
	private SeriesService seriesService;
	@Autowired
	private BdictionaryService bdictionaryService;
	@Autowired
	private BvideoService bvideoService;
	@RequestMapping("listUI.html")
	public String listUI(Model model,Long seriesId, HttpServletRequest request) {
		try
		{
			PageUtil page = new PageUtil();
		   Bdictionary channel=new Bdictionary();
		   SeriesEntity seriesEntity=new SeriesEntity();
			//查询年份
			List<Bdictionary> years=bdictionaryService.selectYear(channel);
			model.addAttribute("list_years",years); 
			//查询地区
			List<Bdictionary> areas=bdictionaryService.selectArea(channel);
			model.addAttribute("list_areas",areas); 
			//查询语言
			List<Bdictionary> language=bdictionaryService.selectLanguage(channel);
			model.addAttribute("list_language",language);
			//查询等级
			List<Bdictionary>Position=bdictionaryService.selectTypeByPosition(channel); 
			model.addAttribute("list_Position",Position);
			 
			List<Channel> channels=BaseData.channels;
			model.addAttribute("list_channel",channels);
		 
			//查询频道分类
			List<Bdictionary> serieKinds = (List<Bdictionary>)getSerieKindsByCode(seriesEntity.getChannel());
			model.addAttribute("list_serieKinds",serieKinds);
			//查询剧情类型
			List<Bdictionary> dramaTypes = (List<Bdictionary>)getDramaTypeByCode(seriesEntity.getChannel());
			model.addAttribute("list_dramaTypes",dramaTypes);
			
		if(request.getParameterMap().containsKey("page")){
			page.setPageNum(Integer.valueOf(request.getParameter("page")));
			page.setPageSize(Integer.valueOf(request.getParameter("rows")));
			page.setOrderByColumn(request.getParameter("sidx"));
			page.setOrderByType(request.getParameter("sord"));
		}
		model.addAttribute("page", page);
		 
			return Common.BACKGROUND_PATH + "/series/list";
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
	}
	
	
	/**
	 * ajax分页动态加载模式
	 * @param dtGridPager Pager对象
	 * @throws Exception
	 */
	@RequestMapping(value = "/list.html", method = RequestMethod.POST)
	@ResponseBody
	public Object list(Model model, Long seriesId, String gridPager, Bdictionary channel, SeriesEntity seriesEntity,
			HttpServletResponse response) throws Exception {
		
		Map<String, Object> parameters = null;
		// 映射Pager对象
		Pager pager = JSON.parseObject(gridPager, Pager.class);
		// 判断是否包含自定义参数
		parameters = pager.getParameters();
		if (parameters.size() < 0) {
			parameters.put("name", null);
		}
		parameters.put("mark","2");
		//3、判断是否是导出操作
		if(pager.getIsExport())
		{
			if(pager.getExportAllData())
			{
				//3.1、导出全部数据
				List<SeriesEntity> list = seriesService.queryListByPage(parameters);
				ExportUtils.exportAll(response, pager, list);
				return null;
			}else
			{
				//3.2、导出当前页数据
				ExportUtils.export(response, pager);
				return null;
			}
		}else
		{
			
			// 设置分页，page里面包含了分页信息
			Page<Object> page = PageHelper.startPage(pager.getNowPage(),pager.getPageSize(), "create_time DESC");
			List<SeriesEntity> list = seriesService.queryListByPage(parameters);
			 
			parameters.clear();
			parameters.put("isSuccess", Boolean.TRUE);
			parameters.put("nowPage", pager.getNowPage());
			parameters.put("pageSize", pager.getPageSize());
			parameters.put("pageCount", page.getPages());
			parameters.put("recordCount", page.getTotal());
			parameters.put("startRecord", page.getStartRow());
			parameters.put("exhibitDatas", list);
			return parameters;
		}
		
	}
	
	
	
	@RequestMapping("addUI.html")
	public String addUI(Model model,Bdictionary channe) {
		try
		{
			//查询频道分类
			List<Bdictionary> serieKinds =bdictionaryService.selectKindByChannel(channe);
			model.addAttribute("list_serieKinds",serieKinds);
			//查询剧情类型
			List<Bdictionary> dramaTypes =bdictionaryService.selectTypeByChannel(channe);
			model.addAttribute("list_dramaTypes",dramaTypes);
			
			List<Bdictionary> channel=bdictionaryService.selectKindByChannel(channe);
			model.addAttribute("list_chan",channel); 
			//查询年份
			List<Bdictionary> years=bdictionaryService.selectYear(channe);
			model.addAttribute("list_years",years); 
			//查询地区
			List<Bdictionary> areas=bdictionaryService.selectArea(channe);
			model.addAttribute("list_areas",areas); 
			//查询语言
			List<Bdictionary> language=bdictionaryService.selectLanguage(channe);
			model.addAttribute("list_language",language); 
			//查询等级
			List<Bdictionary>Position=bdictionaryService.selectTypeByPosition(channe); 
			model.addAttribute("list_Position",Position); 
			
			//查询清晰度
			List<Bdictionary>clarity=bdictionaryService.selectVideoByClarity(channe); 
			model.addAttribute("list_clarity",clarity); 

			//显示和隐藏
			List<Bdictionary>statusy=bdictionaryService.selectByStatus(channe); 
			model.addAttribute("list_statusy",statusy); 
			
			
			List<Channel> channels=BaseData.channels;
			model.addAttribute("list_channel",channels);
			
			return Common.BACKGROUND_PATH + "/series/form";
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
		
	}
	//添加
	@RequestMapping("add.html")
	@ResponseBody
	public Object add(SeriesEntity seriesEntity) throws AjaxException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		List<SeriesEntity> sameSeries=seriesService.queryListByName(seriesEntity.getName());
		
		try
		{
			if(sameSeries.size()>0){
				map.put("success", Boolean.FALSE);
				map.put("data", null);
				map.put("message", "添加失败,有相同剧集名称");
			}else{
				int count=seriesService.insert(seriesEntity);
				 
				if(count >0 )
				{
					map.put("success", Boolean.TRUE);
					map.put("data", null);
					map.put("message", "添加成功");
				}else
				{
					map.put("success", Boolean.FALSE);
					map.put("data", null);
					map.put("message", "添加失败");
				}
			}
		}catch(ServiceException e)
		{
			throw new AjaxException(e);
		}
		return map;
	}
	
	
 

	@RequestMapping("getAllcode.html")
	@ResponseBody
	public Object getAllCode(String code) {
		List<Channel> channels=BaseData.channels;
		for (Channel channel : channels) {
			if(channel.getCode().equals(code)){
				return channel.getAllChannels();
			}
		}
		return null;
	}
	@RequestMapping("getDramaTypes.html")
	@ResponseBody
	public Object getDramaTypeByCode(String code) {
		Bdictionary bdictionary=new Bdictionary();
		bdictionary.setCode(code);
		return bdictionaryService.selectTypeByChannel(bdictionary);
	}
	@RequestMapping("getSerieKinds.html")
	@ResponseBody
	public Object getSerieKindsByCode(String code) {
		Bdictionary bdictionary=new Bdictionary();
		bdictionary.setCode(code);
		return bdictionaryService.selectKindByChannel(bdictionary);
	}
	
	@RequestMapping("editSeoUI.html")
	public String editSeoUI(Model model, HttpServletRequest request, Long id) {
		try
		{
			Bdictionary channel =new Bdictionary();

	        List<Channel> channels=BaseData.channels;
			model.addAttribute("list_channel",channels);
			//查询年份
			List<Bdictionary> years=bdictionaryService.selectYear(channel);
			model.addAttribute("list_years",years); 
			//查询地区
			List<Bdictionary> areas=bdictionaryService.selectArea(channel);
			model.addAttribute("list_areas",areas); 
			//查询语言
			List<Bdictionary> language=bdictionaryService.selectLanguage(channel);
			model.addAttribute("list_language",language);
			//查询等级
			List<Bdictionary>Position=bdictionaryService.selectTypeByPosition(channel); 
			model.addAttribute("list_Position",Position); 
			
            //editUI查询 
			SeriesEntity seriesEntity = seriesService.findById(id);
			//查询频道分类
			
			List<Bdictionary> serieKinds = (List<Bdictionary>)getSerieKindsByCode(seriesEntity.getChannel());
			model.addAttribute("list_serieKinds",serieKinds);
			//查询剧情类型
			 
			List<Bdictionary> dramaTypes = (List<Bdictionary>)getDramaTypeByCode(seriesEntity.getChannel());
			model.addAttribute("list_dramaTypes",dramaTypes);
			
			PageUtil page = new PageUtil();
			page.setPageNum(Integer.valueOf(request.getParameter("page")));
			page.setPageSize(Integer.valueOf(request.getParameter("rows")));
			page.setOrderByColumn(request.getParameter("sidx"));
			page.setOrderByType(request.getParameter("sord"));
			
			model.addAttribute("page", page);
			model.addAttribute("seriesEntity", seriesEntity);
			return Common.BACKGROUND_PATH + "/series/Seoform";
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
	}
	@RequestMapping("editUI.html")
	public String editUI(Model model, HttpServletRequest request, Long id) {
		try
		{
			Bdictionary channel =new Bdictionary();

	        List<Channel> channels=BaseData.channels;
			model.addAttribute("list_channel",channels);
			//查询年份
			List<Bdictionary> years=bdictionaryService.selectYear(channel);
			model.addAttribute("list_years",years); 
			//查询地区
			List<Bdictionary> areas=bdictionaryService.selectArea(channel);
			model.addAttribute("list_areas",areas); 
			//查询语言
			List<Bdictionary> language=bdictionaryService.selectLanguage(channel);
			model.addAttribute("list_language",language);
			//查询等级
			List<Bdictionary>Position=bdictionaryService.selectTypeByPosition(channel); 
			model.addAttribute("list_Position",Position); 
			//查询清晰度
			List<Bdictionary>clarity=bdictionaryService.selectVideoByClarity(channel); 
			model.addAttribute("list_clarity",clarity); 
			//显示和隐藏
			List<Bdictionary>statusy=bdictionaryService.selectByStatus(channel); 
			model.addAttribute("list_statusy",statusy); 
			
            //editUI查询 
			SeriesEntity seriesEntity = seriesService.findById(id);
			//查询频道分类
			 
			List<Bdictionary> serieKinds = (List<Bdictionary>)getSerieKindsByCode(seriesEntity.getChannel());
			model.addAttribute("list_serieKinds",serieKinds);
			//查询剧情类型
			
			List<Bdictionary> dramaTypes = (List<Bdictionary>)getDramaTypeByCode(seriesEntity.getChannel());
			model.addAttribute("list_dramaTypes",dramaTypes);
			System.out.println(dramaTypes.size());
			PageUtil page = new PageUtil();
			page.setPageNum(Integer.valueOf(request.getParameter("page")));
			page.setPageSize(Integer.valueOf(request.getParameter("rows")));
			page.setOrderByColumn(request.getParameter("sidx"));
			page.setOrderByType(request.getParameter("sord"));
			
			model.addAttribute("page", page);
			model.addAttribute("seriesEntity", seriesEntity);
			return Common.BACKGROUND_PATH + "/series/form";
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
	}
	
	//修改
	@RequestMapping("edit.html")
	@ResponseBody
	public Object update(SeriesEntity seriesEntity,Model model) throws AjaxException
	{
		Map<String, Object> map = new HashMap<String, Object>();
	 
		try
		{ 
			int result = seriesService.update(seriesEntity);
			System.out.println(result);
			if(result >0)
			{
				
				map.put("success", Boolean.TRUE);
				map.put("data", null);
				map.put("message", "编辑成功");
				
			}else
			{
				map.put("success", Boolean.FALSE);
				map.put("data", null);
				map.put("message", "编辑失败");
			}
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
		return map;
	}
	
	//刪除
	@RequestMapping("deleteBatch.html")
	@ResponseBody
	public Object deleteBatch(String ids){
		Map<String, Object> result = new HashMap<String, Object>();
		try
		{
			String[] seriesIds = ids.split(",");
			List<Long> list = new ArrayList<Long>();
			for (String string : seriesIds) {
				list.add(Long.valueOf(string));
			}
			int cnt = seriesService.deleteBatchById(list);
			System.out.println(cnt);
			if(cnt == list.size())
			{
				result.put("success", true);
				result.put("data", null);
				result.put("message", "删除成功");
			}else
			{
				result.put("success", false);
				result.put("data", null);
				result.put("message", "删除失败");
			}
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
		return result;
	}
	
	//上线
		@RequestMapping("updateStatus.html")
		@ResponseBody
		public Object updateStatus(String ids){
			System.out.println(ids);
			Map<String, Object> result = new HashMap<String, Object>();
			SeriesEntity entity=new SeriesEntity();
			try
			{
				String[] seriesIds = ids.split(",");
				List<Long> list = new ArrayList<Long>();
				for (String string : seriesIds) {
					list.add(Long.valueOf(string));
				}
				int cnt = seriesService.updateOnline(list);
				if(cnt == list.size())
				{
					result.put("success", true);
					result.put("data", null);
					result.put("message", "上线成功");
				}else
				{
					result.put("success", false);
					result.put("data", null);
					result.put("message", "上线失败");
				}
			}catch(Exception e)
			{
				throw new AjaxException(e);
			}
			return result;
		}
	
		//下线
				@RequestMapping("updateStatus1.html")
				@ResponseBody
				public Object updateStatus1(String ids){
					System.out.println(ids);
					Map<String, Object> result = new HashMap<String, Object>();
					SeriesEntity entity=new SeriesEntity();
					try
					{
						String[] seriesIds = ids.split(",");
						List<Long> list = new ArrayList<Long>();
						for (String string : seriesIds) {
							list.add(Long.valueOf(string));
						}
						int cnt = seriesService.updateOffline(list);
						if(cnt == list.size())
						{
							result.put("success", true);
							result.put("data", null);
							result.put("message", "下线成功");
						}else
						{
							result.put("success", false);
							result.put("data", null);
							result.put("message", "下线失败");
						}
					}catch(Exception e)
					{
						throw new AjaxException(e);
					}
					return result;
				}
	
	@RequestMapping("addBvideoUI.html")
	public String addBvideoUI(Model model, HttpServletRequest request, Long id) {
		 try
		 {
			BvideoEntity bvideoEntity = bvideoService.findById(id);
			PageUtil page = new PageUtil();
			page.setPageNum(Integer.valueOf(request.getParameter("page")));
			page.setPageSize(Integer.valueOf(request.getParameter("rows")));
			page.setOrderByColumn(request.getParameter("sidx"));
			page.setOrderByType(request.getParameter("sord"));
			model.addAttribute("page", page);
			model.addAttribute("bvideoEntity", bvideoEntity);
			return Common.BACKGROUND_PATH + "/series/addBvideo";
		 }catch(Exception e)
			{
				throw new SystemException(e);
			}
		}
	
/*	@RequestMapping("addBvideo.html")
	@ResponseBody
	public Object addBvideos(int roleId, String resourceIds)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		try
		{
			List<Integer> list = new ArrayList<Integer>();
			if(StringUtil.isNotBlank(resourceIds))
			{
				for (String id : resourceIds.split(",")) {
					list.add(Integer.valueOf(id));
				}
			}
			int result = bvideoService.addRolePermBatch(roleId, list);
			if(result>0)
			{
				map.put("success", Boolean.TRUE);
				map.put("data", null);
				map.put("message", "视频资源添加成功");
			}else
			{
				map.put("success", Boolean.FALSE);
				map.put("data", null);
				map.put("message", "视频资源添加失败");
			}
		}catch(Exception e)
		{
			throw new AjaxException(e);
		}
		return map;
	}*/
	
	
}
